package demo2;

import java.util.Arrays;
import java.util.Stack;

public class TestSort {

    /**
     * 计数排序
     * 时间复杂度：O(范围+N)
     * 稳定性：O(范围)
     * 空间复杂度：
     * @param array
     */
    public static void countSort(int[] array) {
        int minVal = array[0];
        int maxVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < minVal) {
                minVal = array[i];
            }
            if (array[i] > maxVal) {
                maxVal = array[i];
            }
        }

        int range = maxVal - minVal + 1;
        int[] count = new int[range];
        for (int i = 0; i < array.length; i++) {
            count[array[i]-minVal]++;
        }

        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index++] = i + minVal;
                count[i]--;
            }
        }
    }

    public static void main(String[] args) {
        //int[] array = {5, 10, 3, 8, 2};
        int[] array = {6, 1, 6, 3};
        System.out.println("排序前：" + Arrays.toString(array));
        countSort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }

    /**
     * 归并排序中的合并
     * @param array
     * @param low
     * @param mid
     * @param high
     */
    private static void merge(int[] array, int low, int mid, int high) {
        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;
        int[] arr = new int[high-low+1];

        int i = 0;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] < array[s2]) {
                arr[i] = array[s1];
                i++;
                s1++;
            }else {
                arr[i++] = array[s2++];
            }
        }

        while (s1 <= e1) {
            arr[i] = array[s1];
            i++;
            s1++;
        }

        while (s2 <= e2) {
            arr[i++] = array[s2++];
        }

        i = 0;
        for (int j = low; j <= high; j++) {
            array[j] = arr[i++];
        }

//        for (int j = 0; j < arr.length; j++) {
//            array[j+low] = arr[j];
//        }
    }

    private static void mergeSortInternal(int[] array, int low, int high) {
        if (low >= high) return;

        int mid = low + ((high-low)>>>1);
        mergeSortInternal(array, low, mid);
        mergeSortInternal(array, mid+1, high);

        merge(array, low, mid, high);
    }

    /**
     * 归并排序
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(N)
     * 稳定性：稳定
     * @param array
     */
    public static void mergeSort(int[] array) {
        mergeSortInternal(array, 0, array.length-1);
    }

    /**
     * 非递归的归并排序
     * @param array
     */
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += 2*gap) {
                int left = i;
                int mid = left + gap - 1;
                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if (right >= array.length) {
                    right = array.length - 1;
                }
                merge(array, left, mid, right);
            }
            gap *= 2;
        }
    }

    public static void main5(String[] args) {
        //int[] array = {5, 10, 3, 8, 2};
        int[] array = {6, 1, 6, 3};
        System.out.println("排序前：" + Arrays.toString(array));
        mergeSortNor(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }

    /**
     * 非递归实现快速排序
     * @param array
     */
    public static void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int pivot = partitonHole(array, left, right);
        if (pivot > left + 1) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if (pivot < right - 1) {
            stack.push(pivot+1);
            stack.push(right);
        }

        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();

            pivot = partitonHole(array, left, right);
            if (pivot > left + 1) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if (pivot < right - 1) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    public static void main4(String[] args) {
        //int[] array = {5, 10, 3, 8, 2};
        int[] array = {6, 1, 6, 3};
        System.out.println("排序前：" + Arrays.toString(array));
        quickSortNor(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }

    private static int partitonHoare(int[] array, int low, int high) {
        int i = low;
        //int tmp = array[low];
        while (low < high) {
            while (low < high && array[high] >= array[i]) {
                high--;
            }

            while (low < high && array[low] <= array[i]) {
                low++;
            }

            swap(array, low, high);
        }
        swap(array, i, low);
        return low;
    }

    /**
     * 挖坑法
     * @param array
     * @param low
     * @param high
     * @return
     */
    private static int partitonHole(int[] array, int low, int high) {
        int tmp = array[low];
        while (low < high) {
            while (low < high && array[high] >= tmp) {
                high--;
            }
            array[low] = array[high];

            while (low < high && array[low] <= tmp) {
                low++;
            }
            array[high] = array[low];
        }
        array[low] = tmp;
        return low;
    }

    /**
     * 双指针法
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partiton(int[] array, int left, int right) {
        int prev = left;
        int cur = prev+1;
        while (cur <= right) {
            if (array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array, prev, cur);
            }
            cur++;
        }
        swap(array, prev, left);
        return prev;
    }

    private static int partiton1(int[] array, int left, int right) {
        int d = left + 1 ;
        int tmp = array[left];
        for (int i = left+1; i <= right; i++) {
            if (array[i] < tmp && array[i] != array[d]) {
                swap(array, d, i);
                d++;
            }
        }
        swap(array, left, d-1);
        return d-1;
    }

    /**
     * 找到三个数的中间大小的下标
     * @param array
     * @param left
     * @param right
     * @return
     */
    public static int midOfThreeIndex(int[] array, int left, int right) {
        int mid = left + (right - left)>>>1;

        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            }else if (array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] < array[right]) {
                return right;
            }else if (array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    private static void quick(int[] array, int left, int right) {
        if (left >= right) return;

        // 优化区间
//        if (right - left - 1 <= 70000) {
//            //insertSort(array);
//            return;
//        }

        // 三数取中法
        int index = midOfThreeIndex(array, left, right);
        swap(array, left, index);

        int pivot = partiton(array, left, right);
        quick(array, left, pivot-1);
        quick(array, pivot+1, right);
    }

    /**
     * 快速排序
     * 时间复杂度：好：O(nlogn)，坏：O(n^2)
     * 空间复杂度：好：O(logn)，坏：O(n)
     * 稳定性：不稳定
     * @param array
     */
    public static void quickSort(int[] array) {
        quick(array, 0, array.length-1);
    }

    /**
     * 冒泡排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length+1; i++) {
            for (int j = 0; j < array.length+1; j++) {
                if (array[j] > array[j+1]) {
                    swap(array, j, j+1);
                }
            }
        }
    }

    public static void bubbleSort2(int[] array) {
        for (int i = 0; i < array.length+1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length+1; j++) {
                if (array[j] > array[j+1]) {
                    swap(array, j, j+1);
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    public static void main3(String[] args) {
        int[] array = {5, 10, 3, 8, 2};
        System.out.println("排序前：" + Arrays.toString(array));
        quickSort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    private static void shiftDown(int[] array, int root, int len) {
        int parent = root;
        int child = 2*parent+1;

        while (child < len) {
            if (child+1 < len && array[child] < array[child+1]) {
                child++;
            }

            if (array[parent] < array[child]) {
                int tmp = array[parent];
                array[parent] = array[child];
                array[child] = tmp;

                parent = child;
                child = 2*parent+1;
            }else{
                break;
            }
        }
    }

    // 大根堆
    public static void createHeap(int[] array) {
        for (int p = (array.length-1-1)/2; p >= 0; p--) {
            shiftDown(array, p, array.length);
        }
    }

    /**
     * 堆排序
     * 时间复杂度：O(n*logn)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void heapSort(int[] array) {
        createHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            shiftDown(array, 0, end);
            end--;
        }
    }

    /**
     * 直接选择排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            int tmp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tmp;
        }
    }

    // 选择排序
    public static void main2(String[] args) {
        int[] array = {5, 10, 3, 8, 2};
        System.out.println("排序前：" + Arrays.toString(array));
        heapSort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }

    /**
     * 希尔排序
     * 时间复杂度：O(n^1.3)-O(n^1.5)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     * @param gap
     */
    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while(gap > 1) {
            shell(array, gap);
            gap /= 2;
        }
        shell(array, 1);
    }

    /**
     * 直接插入排序
     * 时间复杂度：O(n^2)-最坏：O(n^2),最好：O(1)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     * @param array
     */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    // 插入排序
    public static void main1(String[] args) {
        int[] array = {5, 10, 3, 8, 2};
        System.out.println("排序前：" + Arrays.toString(array));
        shellSort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }
}
